//
// Created by 19281121-赵路路@BJTU on 2021/9/17.
//
#pragma once

#include <iostream>
#include <string>

using namespace std;

string Encryption(string str, string k);

string Decryption(string str, string k);

void Virginia() {
    // Preparations
    string plainText;
    string cipherText;
    string decryptedText;
    string key;

    // Inputs
    cout << "Please enter the string you want to encrypt:" << endl;
    cin >> plainText;
    cout << "Please enter the key:" << endl;
    cin >> key;

    //Encrypt
    cipherText = Encryption(plainText, key);
    cout << "The ciphertext is: " << cipherText << endl;

    //Decrypt
    decryptedText = Decryption(cipherText, key);
    cout << "The decryptedtext is: " << decryptedText << endl;
}

string Encryption(string str, string k) {
    char kk[100] = {};
    char kk_length = 0;
    for (int i = 0; i < str.length(); ++i) {
        kk[i] = k[i % k.length()];
        kk_length++;
    }
    for (int i = 0; i < kk_length; ++i) {
        int caesar = kk[i] - 'a';
        str[i] = (str[i] - 'a' + caesar) % 26 + 'a';
    }
    return str;
}

string Decryption(string str, string k) {
    char kk[100] = {};
    char kk_length = 0;
    for (int i = 0; i < str.length(); ++i) {
        kk[i] = k[i % k.length()];
        kk_length++;
    }
    for (int i = 0; i < str.length(); ++i) {
        int caesar = kk[i] - 'a';
        str[i] = str[i] - 'a';
        if (str[i] < caesar) {
            str[i] += 26;
            str[i] = (str[i] - caesar) % 26 + 'a';
            continue;
        }
        str[i] = (str[i] - caesar) % 26 + 'a';
    }
    return str;
}